import { ref } from 'vue';

/**
 * 模态框状态管理hook
 * @param {Object} options - 配置选项
 * @param {boolean} [options.defaultVisible=false] - 默认是否可见
 * @param {Function} [options.beforeOpen] - 打开模态框前的回调函数
 * @param {Function} [options.afterOpen] - 打开模态框后的回调函数
 * @param {Function} [options.beforeClose] - 关闭模态框前的回调函数
 * @returns {Object} 包含visible状态和处理函数的对象
 */
export function useModalState(options = {}) {
  const {
    defaultVisible = false,
    beforeOpen,
    afterOpen,
    beforeClose
  } = options;

  // 模态框可见性状态
  const visible = ref(defaultVisible);

  /**
   * 打开模态框
   * @param {...any} args - 传递给beforeOpen和afterOpen的参数
   */
  const handleOpen = (...args) => {
    if (typeof beforeOpen === 'function') {
      beforeOpen(...args);
    }

    visible.value = true;

    if (typeof afterOpen === 'function') {
      afterOpen(...args);
    }
  };

  /**
   * 关闭模态框
   * @param {...any} args - 传递给beforeClose的参数
   */
  const handleClose = (...args) => {
    if (typeof beforeClose === 'function') {
      beforeClose(...args);
    }

    visible.value = false;
  };

  return {
    visible,
    handleOpen,
    handleClose
  };
}
